from django.shortcuts import render, redirect
from .models import userinfo, virus_record, mail_info, asset
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.utils import timezone
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.core.mail import send_mail, EmailMultiAlternatives
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.views.generic import TemplateView
from django.contrib.auth import authenticate, login, logout
from django.db.models import Q, Count
from django.contrib.auth.hashers import make_password
from django.contrib import auth
from django.contrib.auth.models import User
from django.contrib.auth import views as auth_views
from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
from django.http import JsonResponse
from seclab.settings import per_page
import csv
from .forms import *

# 获取病毒处理记录所有数据,展示info.html


def after_login(request):
    if not request.user.is_authenticated():
        return redirect(reverse('404'))


def index(request):
    total = asset.objects.count()
    return render(request, 'start.html', {'total': total})


def error1(request):
    return render(request, '404.html')


def error2(request):
    return render(request, '500.html')


def info(request):
    info = virus_record.objects.all().order_by('pcip')
    return render(request, 'info.html', {'info': info})

# 获取资产请购所有数据,展示assetinfo.html


def assetinfo(request):
    # 查询asset表所有数据
    assetinfo = asset.objects.all().order_by('id')
    # 使用Paginator模块对数据分页，一页100条数据
    paginator = Paginator(assetinfo, 100)
    # 使用request.GET.get()函数获取uri中的page参数的数值
    page = request.GET.get('page')
    try:
        # 通过获取上面的page参数，查询此page是否为整数并且是否可用
        asset_obj = paginator.page(page)
    except PageNotAnInteger:
        asset_obj = paginator.page(1)
    except (EmptyPage, InvalidPage):
        asset_obj = paginator.page(paginator.num_pages)
    return render(request, 'assetinfo.html', {'asset_list': asset_obj, 'assetinfo': assetinfo})
    # return render(request, 'assetinfo.html', {'asset_list': asset_obj})

# 查询Info表数据cd ..


def searchinfo(request):
    if request.method == 'POST':
        temp_ip = request.POST['pcip']
        search_info = virus_record.objects.filter(pcip=temp_ip)
        return render(request, 'search.html', {'searchinfo': search_info})

# 查询asset表数据


def searchassetinfo(request):
    if request.method == 'POST':
        temp_Requisitioner = request.POST['Requisitioner']
        temp_Department = request.POST['Department']
        temp_Document_No = request.POST['Document_No']
        temp_Materials = request.POST['Materials']
        if temp_Requisitioner:
            search_assetinfo = asset.objects.filter(
                Requisitioner=temp_Requisitioner)
        if temp_Department:
            search_assetinfo = asset.objects.filter(Department=temp_Department)
        if temp_Document_No:
            search_assetinfo = asset.objects.filter(
                Document_No=temp_Document_No)
        if temp_Materials:
            search_assetinfo = asset.objects.filter(Materials=temp_Materials)
        return render(request, 'searchasset.html', {'searchassetinfo': search_assetinfo})


# 新增病毒处理记录


def addinfo(request):
    if request.method == 'POST':
        temp_pcip = request.POST['pcip']
        temp_username = request.POST['username']
        temp_number = request.POST['number']
        temp_pcname = request.POST['pcname']
        temp_status = request.POST['status']
        temp_handle_time = request.POST['handle_time']
        temp_remarks = request.POST['remarks']
        temp_virus_type = request.POST['virus_type']
        temp_pcmac = request.POST['pcmac']

    temp_info = virus_record(
        pcip=temp_pcip,
        username=temp_username,
        number=temp_number,
        pcname=temp_pcname,
        status=temp_status,
        handle_time=temp_handle_time,
        remarks=temp_remarks,
        virus_type=temp_virus_type,
        pcmac=temp_pcmac,
    )
    temp_info.save()
    # 操作记录添加到日志中
    # 重定向
    return HttpResponseRedirect(reverse('info'))


# 删除病毒处理记录

def deleteinfo(request, info_id):
    infoID = info_id
    virus_record.objects.filter(id=infoID).delete()
    return HttpResponseRedirect(reverse('info'))

# 修改资产请购表数据


def showdetail(request):
    return render(request, 'changedetail.html')


def cgdetail(request):
    if request.method == 'POST':
        temp_id = request.POST['id']
        temp_assetinfo = asset.objects.filter(
            id=temp_id)
        return render(request, 'changedetail.html', {'cgassetinfo': temp_assetinfo})


def submitdetail(request):
    if request.method == 'POST':
        temp_id = request.POST.get('id')
        temp_Finish = request.POST.get('Finish')
        temp_Source = request.POST.get('Source')
        temp_Document_No = request.POST.get('Document_No')
        temp_Order_No = request.POST.get('Order_No')
        temp_Requisition_date = request.POST.get('Requisition_date')
        temp_Materials = request.POST.get('Materials')
        temp_Department = request.POST.get('Department')
        temp_Requisitioner = request.POST.get('Requisitioner')
        temp_Requisition_Num = request.POST.get('Requisition_Num')
        temp_Requisition_purpose = request.POST.get('Requisition_purpose')
        temp_Order_Time = request.POST.get('Order_Time')
        temp_Purchaser = request.POST.get('Purchaser')
        temp_Reason = request.POST.get('Reason')
        temp_Supplier = request.POST.get('Supplier')
        temp_Arrival_time1 = request.POST.get('Arrival_time1')
        temp_Arrival_time2 = request.POST.get('Arrival_time2')
        temp_Actual_time1 = request.POST.get('Actual_time1')
        temp_Actual_Num1 = request.POST.get('Actual_Num1')
        temp_Actual_time2 = request.POST.get('Actual_time2')
        temp_Actual_Num2 = request.POST.get('Actual_Num2')
        temp_Actual_time3 = request.POST.get('Actual_time3')
        temp_Actual_Num3 = request.POST.get('Actual_Num3')
        temp_Unarrival_Num = request.POST.get('Unarrival_Num')
        temp_Overdue = request.POST.get('Overdue')
        temp_Overdue_time = request.POST.get('Overdue_time')
        temp_Remarks = request.POST.get('Remarks')

    temp_asset = asset(
        id=temp_id,
        Finish=temp_Finish,
        Source=temp_Source,
        Document_No=temp_Document_No,
        Order_No=temp_Order_No,
        Requisition_date=temp_Requisition_date,
        Materials=temp_Materials,
        Department=temp_Department,
        Requisitioner=temp_Requisitioner,
        Requisition_Num=temp_Requisition_Num,
        Requisition_purpose=temp_Requisition_purpose,
        Order_Time=temp_Order_Time,
        Purchaser=temp_Purchaser,
        Reason=temp_Reason,
        Supplier=temp_Supplier,
        Arrival_time1=temp_Arrival_time1,
        Arrival_time2=temp_Arrival_time2,
        Actual_time1=temp_Actual_time1,
        Actual_Num1=temp_Actual_Num1,
        Actual_time2=temp_Actual_time2,
        Actual_Num2=temp_Actual_Num2,
        Actual_time3=temp_Actual_time3,
        Actual_Num3=temp_Actual_Num3,
        Unarrival_Num=temp_Unarrival_Num,
        Overdue=temp_Overdue,
        Overdue_time=temp_Overdue_time,
        Remarks=temp_Remarks,

    )
    temp_asset.save(force_update=True)
    '''
    a = models.asset.objects.filter(id=temp_id).update(Finish=temp_Finish,
        Source=temp_Source,
        Document_No=temp_Document_No,
        Order_No=temp_Order_No,
        Requisition_date=temp_Requisition_date,
        Materials=temp_Materials,
        Department=temp_Department,
        Requisitioner=temp_Requisitioner,
        Requisition_Num=temp_Requisition_Num,
        Requisition_purpose=temp_Requisition_purpose,
        Order_Time=temp_Order_Time,
        Purchaser=temp_Purchaser,
        Reason=temp_Reason,
        Supplier=temp_Supplier,
        Arrival_time1=temp_Arrival_time1,
        Arrival_time2=temp_Arrival_time2,
        Actual_time1=temp_Actual_time1,
        Actual_Num1=temp_Actual_Num1,
        Actual_time2=temp_Actual_time2,
        Actual_Num2=temp_Actual_Num2,
        Actual_time3=temp_Actual_time3,
        Actual_Num3=temp_Actual_Num3,
        Unarrival_Num=temp_Unarrival_Num,
        Overdue=temp_Overdue,
        Overdue_time=temp_Overdue_time,
        Remarks=temp_Remarks)
    '''
    # 重定向
    return HttpResponseRedirect(reverse('showdetail'))


def changedetail(request, asset_id):
    assetid = asset_id
    #assetid = int(request.POST.get('asset_id'))
    tempasset = asset.objects.filter(id=assetid).first()
    asset_form = assetForm(request.POST)
    # 判断表单是否正确
    # if request.method == "POST":
    if asset_form.is_valid():
        tempasset.Finish = request.POST.get('Finish')
        tempasset.Source = request.POST.get('Source')
        tempasset.Document_No = request.POST.get('Document_No')
        tempasset.Order_No = request.POST.get('Order_No')
        tempasset.Requisition_date = request.POST.get('Requisition_date')
        tempasset.Materials = request.POST.get('Materials')
        tempasset.Department = request.POST.get('Department')
        tempasset.Requisition_Num = request.POST.get('Requisition_Num')
        tempasset.Requisition_purpose = request.POST.get('Requisition_purpose')
        tempasset.Order_Time = request.POST.get('Order_Time')
        tempasset.Purchaser = request.POST.get('Purchaser')
        tempasset.Reason = request.POST.get('Reason')
        tempasset.Supplier = request.POST.get('Supplier')
        tempasset.Arrival_time1 = request.POST.get('Arrival_time1')
        tempasset.Arrival_time2 = request.POST.get('Arrival_time2')
        tempasset.Actual_time1 = request.POST.get('Actual_time1')
        tempasset.Actual_Num1 = request.POST.get('Actual_Num1')
        tempasset.Actual_time2 = request.POST.get('Actual_time2')
        tempasset.Actual_Num2 = request.POST.get('Actual_Num2')
        tempasset.Actual_time3 = request.POST.get('Actual_time3')
        tempasset.Actual_Num3 = request.POST.get('Actual_Num3')
        tempasset.Unarrival_Num = request.POST.get('Unarrival_Num')
        tempasset.Overdue = request.POST.get('Overdue')
        tempasset.Overdue_time = request.POST.get('Overdue_time')
        tempasset.Remarks = request.POST.get('Remarks')
        # tempasset.save()
        return render(request, 'changedetail.html', {'Finish': tempasset.Finish, 'Source': tempasset.Source, 'Document_No': tempasset.Document_No, 'Order_No': tempasset.Order_No, 'Requisition_date': tempasset.Requisition_date, 'Materials': tempasset.Materials, 'Department': tempasset.Department, 'Requisitioner': tempasset.Requisitioner, 'Requisition_Num': tempasset.Requisition_Num,
                                                     'Requisition_purpose': tempasset.Requisition_purpose, 'Order_Time': tempasset.Order_Time, 'Purchaser': tempasset.Purchaser, 'Reason': tempasset.Reason, 'Supplier': tempasset.Supplier, 'Arrival_time1': tempasset.Arrival_time1, 'Arrival_time2': tempasset.Arrival_time2, 'Actual_time1': tempasset.Actual_time1, 'Actual_Num1': tempasset.Actual_Num1,
                                                     'Actual_time2': tempasset.Actual_time2, 'Actual_Num2': tempasset.Actual_Num2, 'Actual_time3': tempasset.Actual_time3, 'Actual_Num3': tempasset.Actual_Num3, 'Unarrival_Num': tempasset.Unarrival_Num, 'Overdue': tempasset.Overdue, 'Overdue_time': tempasset.Overdue_time, 'Remarks': tempasset.Remarks})
# 删除物资请购数据


def delassetinfo(request, assetinfo_id):
    assetinfoID = assetinfo_id
    asset.objects.filter(id=assetinfoID).delete()
    return HttpResponseRedirect(reverse('showasset'))


# 发送告警邮件

def sendmail(request):
    if request.method == 'POST':
        subject = '笔记本中毒告警'
        text_content = '你好，你的笔记本中了挖矿病毒，需要立即到C栋1.5楼信息部处理！'
        from_email = 'yiwan.jiang@leadchina.cn'
        recipient_list = request.POST['mailaddress']
        send_mail(subject, text_content, from_email, [
                  recipient_list], fail_silently=False)
        return HttpResponse('邮件发送成功')
    else:
        return HttpResponse('邮件发送失败')

# 展示sendmail.html


def showsendmail(request):
    return render(request, 'mail.html')


# 展示新增资产请购数据网页

def showaddasset(request):
    return render(request, 'addasset.html')

# 展示查询信息表


def showsearch(request):
    return render(request, 'search.html')

# 展示查询物资请购信息页面


def showqueryasset(request):
    return render(request, 'searchasset.html')

# showbase


def showbase(request):
    return render(request, 'base.html')

# 展示首页


def showstart(request):
    return render(request, 'start.html')


class IndexView(TemplateView):
    template_name = 'start.html'

    def get(self, request, *args, **kwargs):
        return super(IndexView, self).get(request, *args, **kwargs)


# 新增资产请购数据

def addasset(request):
    if request.method == 'POST':
        temp_Finish = request.POST['Finish']
        temp_Source = request.POST['Source']
        temp_Document_No = request.POST['Document_No']
        temp_Order_No = request.POST['Order_No']
        temp_Requisition_date = request.POST['Requisition_date']
        temp_Materials = request.POST['Materials']
        temp_Department = request.POST['Department']
        temp_Requisitioner = request.POST['Requisitioner']
        temp_Requisition_Num = request.POST['Requisition_Num']
        temp_Requisition_purpose = request.POST['Requisition_purpose']
        temp_Order_Time = request.POST['Order_Time']
        temp_Purchaser = request.POST['Purchaser']
        temp_Reason = request.POST['Reason']
        temp_Supplier = request.POST['Supplier']
        temp_Arrival_time1 = request.POST['Arrival_time1']
        temp_Arrival_time2 = request.POST['Arrival_time2']
        temp_Actual_time1 = request.POST['Actual_time1']
        temp_Actual_Num1 = request.POST['Actual_Num1']
        temp_Actual_time2 = request.POST['Actual_time2']
        temp_Actual_Num2 = request.POST['Actual_Num2']
        temp_Actual_time3 = request.POST['Actual_time3']
        temp_Actual_Num3 = request.POST['Actual_Num3']
        temp_Unarrival_Num = request.POST['Unarrival_Num']
        temp_Overdue = request.POST['Overdue']
        temp_Overdue_time = request.POST['Overdue_time']
        temp_Remarks = request.POST['Remarks']

    temp_asset = asset(
        Finish=temp_Finish,
        Source=temp_Source,
        Document_No=temp_Document_No,
        Order_No=temp_Order_No,
        Requisition_date=temp_Requisition_date,
        Materials=temp_Materials,
        Department=temp_Department,
        Requisitioner=temp_Requisitioner,
        Requisition_Num=temp_Requisition_Num,
        Requisition_purpose=temp_Requisition_purpose,
        Order_Time=temp_Order_Time,
        Purchaser=temp_Purchaser,
        Reason=temp_Reason,
        Supplier=temp_Supplier,
        Arrival_time1=temp_Arrival_time1,
        Arrival_time2=temp_Arrival_time2,
        Actual_time1=temp_Actual_time1,
        Actual_Num1=temp_Actual_Num1,
        Actual_time2=temp_Actual_time2,
        Actual_Num2=temp_Actual_Num2,
        Actual_time3=temp_Actual_time3,
        Actual_Num3=temp_Actual_Num3,
        Unarrival_Num=temp_Unarrival_Num,
        Overdue=temp_Overdue,
        Overdue_time=temp_Overdue_time,
        Remarks=temp_Remarks,

    )
    temp_asset.save()
    # 重定向
    return HttpResponseRedirect(reverse('showaddasset'))

    # 用户管理
    # 展示login页面


def login(request):
    return render(request, 'login.html')

    # 用户登录


def userlogin(request):
    if request.method == "POST":
        user1 = request.POST.get('username')
        password1 = request.POST.get('password')
        # result = authenticate(username=user1,password=password1) #通过django自带用户管理模块验证

        # 通过user库的密码验证
        result = userinfo.objects.get(
            username=user1,
            password=password1,
        )

    if not result:
        # return HttpResponseRedirect('/register/')
        return render(request, 'login', {
            'msg': '用户名密码错误'
        })
    else:
        request.session['user'] = userinfo
        # return HttpResponseRedirect('/start/') #使用HttpResponseRedirect
        # return render(request, 'start.html')  使用return render
        return redirect(reverse('start'))


def userlogout(request):
    logout(request)
    return redirect(reverse('login'))


# 用户修改密码
def pwdmodify(request):
    return render(request, 'user_pwd_modify.html')


def changepwd(request):
    if request.method == 'POST':
        user_pwd_modify_form = UserPwdModifyForm(request.POST)
        if user_pwd_modify_form.is_valid():
            user1 = request.POST.get('usernum1').strip()
            #user1 = request.user.username
            pwd1 = request.POST.get('pwd1').strip()
            pwd2 = request.POST.get('pwd2').strip()
            if pwd1 == pwd2:
                userinfo.objects.filter(username=user1).update(password=pwd1)
                #User.objects.filter(username = user1).update(password = pwd1)
                #user1.password = make_password(pwd1)
                # user1.save()
                return HttpResponseRedirect((reverse('login')))
            else:
                return render(request, 'user_pwd_modify.html', {'msg': '两次密码不一致！'})
        else:
            return render(request, 'user_pwd_modify.html', {'msg': '密码不能为空！',
                                                            'user_pwd_modify_form': user_pwd_modify_form})


# 资产列表导出


